package a;

//优化:匿名内部类结合使用

public class DemoTest2 {
    public static void main(String[] args) {
        //局部内部类
        class MyInner extends Thread{
            @Override
            public void run(){
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                }
            }
        }

        MyInner inner = new MyInner();
        inner.start();

        System.out.println("-------------------------------------");

        //匿名内部类结合Thread使用

        Thread t1 = new Thread() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
            }
        }
    };
        t1.start();

        //匿名对象开启线程
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                }
            }
        }.start();

        System.out.println("---------------------------------");
        //匿名内部类结合Runnable实现
        //任务类: 一个任务类可以多次重复使用
        Runnable r = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                }
            }
        };
        Thread t2 = new Thread(r);
        t2.start();

        //优化
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                }
            }
        }).start();
    }
}
